package com.wsoft.system.controller;

import com.wsoft.response.ResponsePage;
import com.wsoft.response.ResponseResult;
import com.wsoft.system.entity.SysMemberEntity;
import com.wsoft.system.entity.SysRoleEntity;
import com.wsoft.system.query.member.MemberGetQuery;
import com.wsoft.system.query.role.RoleInsertQuery;
import com.wsoft.system.query.role.RoleListQuery;
import com.wsoft.system.query.role.RoleUpdateQuery;
import com.wsoft.system.service.ISysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 角色表 前端控制器
 * @author juiet
 * @since 2024年09月22日 14:00:05
 */
@RestController
@Tag(name = "角色管理")
@RequestMapping("/sys/role")
public class SysRoleController {
    @Resource
    private ISysRoleService roleService;
    @Operation(summary = "新增角色",description = "新增角色")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid RoleInsertQuery query) {
        boolean b = roleService.insert(query);
        if (b){
            return ResponseResult.success("[sys.msg.success]");
        }
        else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "编辑角色",description = "编辑角色")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid RoleUpdateQuery query) {
        boolean b = roleService.update(query);
        if (b){
            return ResponseResult.success("[sys.msg.success]");
        }
        else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "删除角色",description = "删除角色")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam("id") Long id) {
        boolean b = roleService.delete(id);
        if (b){
            return ResponseResult.success("[sys.msg.success]");
        }
        else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "查看角色详情",description = "查看角色详情")
    @GetMapping("/get")
    public ResponseResult getRole(@RequestParam("id") Long id) {
        return ResponseResult.success(roleService.getRole(id));
    }

    @Operation(summary = "角色列表（分页）",description = "分页")
    @PostMapping("/list")
    public ResponseResult pageList(@RequestBody @Valid RoleListQuery query) {
        return ResponseResult.success(ResponsePage.restPage(roleService.pageRole(query)));
    }

    @Operation(summary = "批量获取角色信息",description = "批量获取角色信息")
    @PostMapping("/getByIds")
    public ResponseResult<List<SysRoleEntity>> getByIds(@RequestBody @Valid MemberGetQuery query) {
        List<SysRoleEntity> list = roleService.listByIds(query.getIds());
        return ResponseResult.success(list);
    }
}
